package com.example.hibernate.program;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class Driver {

	public static void main(String[] args) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		try {
//			Employee employee = new Employee();
//			employee.setFirstName("Önder");
//			employee.setLastName("ŞAHİN");
//			employee.setEmail("crazyboy1234@gmail.com");
//			{
//				Department department = new Department();
//				department.setName("Java");
//				employee.setDepartment(department);
//			}
//			session.save(employee);
//			Employee employee = new Employee();          
//			employee.setFirstName("Koray");              
//			employee.setLastName("ALTUNDAŞ");               
//			employee.setEmail("excalibur_61@gmail.com"); 
//			{                                            
//				Department department = (Department) session.get(Department.class, 2L);
//				employee.setDepartment(department);
//			}                                            
//			session.save(employee);               
			
			Department department = (Department) session.get(Department.class, 2L);
			Set<Employee> employees = department.getEmployees();
			Iterator<Employee> iterator = employees.iterator();
			while (iterator.hasNext()) {
				Employee employee = (Employee) iterator.next();
				System.out.println(employee);
			}
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
		}
		session.close();
		sessionFactory.close();
	}
}
